//
//  MainTabBarController.m
//  YYFMMall
//
//  Created by YuYou on 2019/8/19.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "MainTabBarController.h"
#import "AppHomeVC.h"
#import "FirstHomeVC.h"
#import "SecondHomeVC.h"
#import "ThirdHomeVC.h"
#import "MyHomeVC.h"

@interface MainTabBarController () <UITabBarControllerDelegate>

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setChildVC:[[AppHomeVC alloc] init]
               title:@"首页"
               image:@"tabBar_home_nor"
       selectedImage:@"tabBar_home_sel"];
    [self setChildVC:[[FirstHomeVC alloc] init]
            title:@"团购"
            image:@"tabBar_tuangou_nor"
    selectedImage:@"tabBar_tuangou_sel"];
    [self setChildVC:[[SecondHomeVC alloc] init]
               title:@"邻里"
               image:@"tabBar_linli_nor"
       selectedImage:@"tabBar_linli_sel"];
    
    [self setChildVC:[[ThirdHomeVC alloc] init]
               title:@"周边"
               image:@"tabBar_zhoubian_nor"
       selectedImage:@"tabBar_zhoubian_sel"];
    
    [self setChildVC:[[MyHomeVC alloc] init]
               title:@"我的"
               image:@"tabBar_me_nor"
       selectedImage:@"tabBar_me_sel"];
    
    //调整字体的位置
    [UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -3);
    self.delegate = self;
    [[UITabBar appearance] setTranslucent:NO];//解决返回图标卡顿
    
}


static NSInteger historyIndex = 0;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex != historyIndex) {
        historyIndex = tabBarController.selectedIndex;
        [self animationWithIndex:tabBarController.selectedIndex];//图片文字都动
    }
}


#pragma mark- private

/// 点击动画
- (void)animationWithIndex:(NSInteger)index {
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration     = 0.2;
    pulse.repeatCount  = 1;
    pulse.autoreverses = YES;
    pulse.fromValue    = @(0.8);
    pulse.toValue      = @(1.3);
    [[(UIView *)tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
}

/// 创建子控制器
- (void)setChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)str selectedImage:(NSString *)str1 {
    
    childVC.tabBarItem.title = title;
    
    [childVC.tabBarItem setImage:[kImage(str) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [childVC.tabBarItem setSelectedImage:[kImage(str1) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kBlackColor,NSFontAttributeName:kFont(10)} forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor,NSFontAttributeName:kFont(10)} forState:UIControlStateSelected];
    
    if (@available(iOS 13.0, *)) {
        self.tabBar.tintColor = kMainColor;
        self.tabBar.unselectedItemTintColor = kBlackColor;
    }
    RTRootNavigationController *nav = [[RTRootNavigationController alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
}




@end
